﻿using System;
using System.Drawing;
using System.Windows.Forms;
using MobileCarManager.MobileControls.Styles;
using OpenNETCF.GDIPlus;

namespace MobileCarManager.MobileControls
{
  /// <summary>Implements a stylable view base class.</summary>
  public partial class View : UserControl
  {
    /// <summary>Constructor.</summary>
    public View()
    {
      InitializeComponent();
    }

    /// <summary>Returns the root element of a button style.</summary>
    private MobileCarManager.MobileControls.Styles.ViewStyle Styles
    {
      get { return StyleManager.Instance.Styles.View; }
    }

    /// <summary>
    /// Handler for the paint event. Draws the view with a stylable background.
    /// </summary>
    protected override void OnPaint(PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      DrawBackground(g);

      base.OnPaint(e);
    }

    /// <summary>Draws the background of the button.</summary>
    private void DrawBackground(Graphics graphics)
    {
      // Get the colors of the background.
      Color[] colors = Styles.BackgroundGradient.Gradient.ToArray();

      IntPtr hdc = graphics.GetHdc();
      using (LinearGradientBrush brush = new LinearGradientBrush(new GpPointF(0, 0), new GpPointF(0, this.Height), colors[0], colors[1]))
      using (GraphicsPlus gp = new GraphicsPlus(hdc))
      {
        gp.FillRectangle(brush, new GpRect(0, 0, this.Width, this.Height));
      }
      graphics.ReleaseHdc(hdc);
    }
  }
}
